import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.ui.TextAnchor;

public class ChartCustomizer implements JRChartCustomizer {

	public void customize(JFreeChart chart, JRChart jasperChart) {

		java.awt.Font tamLetra = new java.awt.Font("Arial", Font.PLAIN, 7);
		java.awt.Color sombra = new java.awt.Color(165, 165, 165);

		if (jasperChart.getChartType() == JRChart.CHART_TYPE_BAR3D) {

			BarRenderer renderer = (BarRenderer) chart.getCategoryPlot()
					.getRenderer();

			CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();

			ValueAxis valueAxis = plot.getRangeAxis();

			CategoryItemRenderer line = plot.getRenderer();

			// renderer.setDrawBarOutline(true);
			// renderer.setOutlinePaint(Color.black);
			// renderer.setOutlineStroke(new BasicStroke(0.1f));
			renderer.setMaximumBarWidth(0.9f);
			renderer.setItemMargin(-0.85f);
			renderer.setItemLabelAnchorOffset(14);
			renderer.setItemLabelFont(tamLetra);

			valueAxis.setUpperMargin(-3.40);

			line.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
			line.setBaseItemLabelsVisible(true);
			line.setBaseItemLabelPaint(Color.BLACK);
			ItemLabelPosition p = new ItemLabelPosition(
					ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT,
					TextAnchor.CENTER_LEFT, 0.0);
			line.setBasePositiveItemLabelPosition(p);
			;

			Locale locale = new Locale("en", "UK");
			String pattern = "###0.00";

			DecimalFormat decimalFormat = (DecimalFormat) NumberFormat
					.getNumberInstance(locale);
			decimalFormat.applyPattern(pattern);

			// DecimalFormat dfKey = new DecimalFormat("###0.00");

			StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator(
					"{2}", decimalFormat);

			renderer.setBaseItemLabelGenerator(labelGenerator);

			/*
			 * 
			 * {0} - label would be equal to Series expression, {1} - label
			 * would be equal to Category expression, {2} - label would be equal
			 * to Value expression
			 */

			/*
			 * NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
			 * DecimalFormat currencyFormatter = new
			 * DecimalFormat("$###,##0.00");
			 * numAxis.setNumberFormatOverride(currencyFormatter);
			 */

			// renderer.setSeriesPaint(0, Color.MAGENTA);
			// renderer.setSeriesPaint(1, Color.orange);
		}

		if (jasperChart.getChartType() == JRChart.CHART_TYPE_BAR) {

			BarRenderer renderer = (BarRenderer) chart.getCategoryPlot()
					.getRenderer();

			CategoryPlot categoryPlot = (CategoryPlot) chart.getCategoryPlot();

			ValueAxis valueAxis = categoryPlot.getRangeAxis();

			renderer.setMaximumBarWidth(0.1f);
			renderer.setItemMargin(-4.3f);
			renderer.setItemLabelFont(tamLetra);

			valueAxis.setUpperMargin(11.40);
			
			Locale locale = new Locale("en", "UK");
			String pattern = "###0.00";

			DecimalFormat decimalFormat = (DecimalFormat) NumberFormat
					.getNumberInstance(locale);
			decimalFormat.applyPattern(pattern);

			StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator(
					"{2}", decimalFormat);

			renderer.setBaseItemLabelGenerator(labelGenerator);

			/*
			 * DecimalFormat dfKey = new DecimalFormat("#####0.00");
			 * 
			 * StandardCategoryItemLabelGenerator labelGenerator = new
			 * StandardCategoryItemLabelGenerator("{2}", dfKey);
			 * 
			 * renderer.setBaseItemLabelGenerator(labelGenerator);
			 * 
			 * 
			 * 
			 * {0} - label would be equal to Series expression, {1} - label
			 * would be equal to Category expression, {2} - label would be equal
			 * to Value expression
			 */

			/*
			 * NumberAxis rangeAxis = (NumberAxis) categoryPlot.getRangeAxis();
			 * rangeAxis.setTickLabelFont(new Font("Arial",Font.BOLD,12));
			 * rangeAxis.setTickUnit(new NumberTickUnit(1000, new
			 * DecimalFormat("$##,##,##0")));
			 */

			/*
			 * CategoryPlot vcPlot = (CategoryPlot) chart.getPlot(); NumberAxis
			 * numAxis = (NumberAxis) vcPlot.getRangeAxis(); DecimalFormat
			 * currencyFormatter = new DecimalFormat("$###,##0.00");
			 * numAxis.setNumberFormatOverride(currencyFormatter);
			 */

			/*
			 * renderer.setShadowVisible(true); renderer.setShadowPaint(sombra);
			 * renderer.setShadowXOffset(4);
			 */
		}

		if (jasperChart.getChartType() == JRChart.CHART_TYPE_LINE) {

			CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();

			CategoryAxis domainAxis = plot.getDomainAxis();

			plot.setRangeGridlinesVisible(false);
			plot.setDomainGridlinesVisible(false);
			plot.getRenderer().setSeriesStroke(0, new BasicStroke(1.5f));
			plot.getRenderer().setSeriesStroke(1, new BasicStroke(1.5f));

			domainAxis.setVisible(false);
			domainAxis.setAxisLineVisible(false);
			domainAxis.setMinorTickMarksVisible(false);
			domainAxis.setTickLabelsVisible(false);
			domainAxis.setTickMarksVisible(false);
			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
			domainAxis.setLowerMargin(2.822);
			domainAxis.setUpperMargin(2.0);

			LegendTitle legend = chart.getLegend();
			legend.setBorder(BlockBorder.NONE);

			/*
			 * IntervalMarker target = new IntervalMarker(6.5, 7.5);
			 * //target.setLabel("Target Range"); //target.setLabelFont(new
			 * Font("SansSerif", Font.ITALIC, 11));
			 * target.setLabelAnchor(RectangleAnchor.LEFT);
			 * target.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
			 * target.setPaint(new Color(222, 222, 255, 128));
			 * plot.addRangeMarker(target, Layer.BACKGROUND);
			 */

			// domainAxis.setUpperMargin(0);
			// domainAxis.setLowerMargin(0);

			// customize the range axis
			/*
			 * NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			 * rangeAxis.setVisible(false); rangeAxis.setAutoRange(true);
			 * rangeAxis.setAutoRangeIncludesZero(false);
			 * rangeAxis.setUpperMargin(0); rangeAxis.setLowerMargin(0);
			 */

			/*
			 * LineAndShapeRenderer lineAndShapeRenderer =(LineAndShapeRenderer)
			 * plot.getRenderer(); Stroke dashed = new
			 * BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
			 * 10.0f, new float[] {10.0f}, 0.0f);
			 * lineAndShapeRenderer.setBaseStroke(dashed);
			 * lineAndShapeRenderer.setBaseItemLabelsVisible(false);
			 * lineAndShapeRenderer.setSeriesStroke( 0, new BasicStroke( 1.5f,
			 * BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[]
			 * {4.0f, 2.0f}, 0.0f ) );
			 */
		}
	}
}
